package com.yunwaikeji.gy_tool_library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.info.GYDisplayInfo;
import com.yunwaikeji.gy_tool_library.info.GYImageInfo;
import com.yunwaikeji.gy_tool_library.interface1.GYViewBgInterface;
import com.yunwaikeji.gy_tool_library.interface1.GYViewSetInterface;
import com.yunwaikeji.gy_tool_library.set.GYViewBgBuilder;
import com.yunwaikeji.gy_tool_library.set.GYViewSetSet;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;


public class GYImageView extends AppCompatImageView implements GYViewBgInterface<GYImageView, GYImageView.GYViewBgBuilder1>, GYViewSetInterface<GYImageView, GYImageView.GYViewSet>, GYImageInfo {
    public static GYImageView newInstance(Context context) {
        return new GYImageView(context);
    }

    public GYImageView(@NonNull Context context) {
        super(context);
        initView(null);
    }

    public GYImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public GYImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    @Override
    public void initView(AttributeSet attributeSet) {
        getBackgroundBuilder(attributeSet).build();
        int imageRadiusPx = WRAP_CONTENT;
        if (attributeSet != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.GYImageView);
            getImageBuilder().imageSrc = typedArray.getDrawable(R.styleable.GYImageView_android_src);
            getImageBuilder().imageDisplayStyle = typedArray.getInteger(R.styleable.GYImageView_imageDisplayStyle, getImageBuilder().imageDisplayStyle);
            getImageBuilder().imageEdgeWidthPx = typedArray.getDimensionPixelSize(R.styleable.GYImageView_imageEdgeWidth, getImageBuilder().imageEdgeWidthPx);
            getImageBuilder().imageEdgeColor = typedArray.getColor(R.styleable.GYImageView_imageEdgeColor, getImageBuilder().imageEdgeColor);
            getImageBuilder().imageEdgeOver = typedArray.getBoolean(R.styleable.GYImageView_imageEdgeOver, getImageBuilder().imageEdgeOver);
            imageRadiusPx = typedArray.getDimensionPixelSize(R.styleable.GYImageView_imageRadius, imageRadiusPx);
            getImageBuilder().imageRadiusLeftTopPx = typedArray.getDimensionPixelSize(R.styleable.GYImageView_imageRadiusLeftTop, getImageBuilder().imageRadiusLeftTopPx);
            getImageBuilder().imageRadiusRightTopPx = typedArray.getDimensionPixelSize(R.styleable.GYImageView_imageRadiusRightTop, getImageBuilder().imageRadiusRightTopPx);
            getImageBuilder().imageRadiusRightBottomPx = typedArray.getDimensionPixelSize(R.styleable.GYImageView_imageRadiusRightBottom, getImageBuilder().imageRadiusRightBottomPx);
            getImageBuilder().imageRadiusLeftBottomPx = typedArray.getDimensionPixelSize(R.styleable.GYImageView_imageRadiusLeftBottom, getImageBuilder().imageRadiusLeftBottomPx);
            typedArray.recycle();
        }
        if (imageRadiusPx >= 0) {
            getImageBuilder().imageRadiusLeftTopPx = imageRadiusPx;
            getImageBuilder().imageRadiusRightTopPx = imageRadiusPx;
            getImageBuilder().imageRadiusRightBottomPx = imageRadiusPx;
            getImageBuilder().imageRadiusLeftBottomPx = imageRadiusPx;
        }
        getImageBuilder().build();
    }

    @Override
    public GYViewBgBuilder1 setBackgroundBuilder(GYViewBgBuilder1 gyViewBgBuilder1) {
        viewBgBuilder1 = gyViewBgBuilder1;
        return viewBgBuilder1;
    }

    private GYViewBgBuilder1 viewBgBuilder1;

    @Override
    public GYViewBgBuilder1 getBackgroundBuilder() {
        return getBackgroundBuilder(null);
    }

    @Override
    public GYViewBgBuilder1 getBackgroundBuilder(AttributeSet attributeSet) {
        if (viewBgBuilder1 == null) {
            viewBgBuilder1 = new GYViewBgBuilder1(this, attributeSet);
        }
        return viewBgBuilder1;
    }

    private GYViewSet viewSet;

    @Override
    public GYViewSet getViewSet() {
        if (viewSet == null) {
            viewSet = new GYViewSet(this);
        }
        return viewSet;
    }

    public static class GYViewBgBuilder1 extends GYViewBgBuilder<GYImageView, GYViewBgBuilder1> {
        public GYViewBgBuilder1(View view) {
            super(view);
        }

        public GYViewBgBuilder1(View view, AttributeSet attributeSet) {
            super(view, attributeSet);
        }
    }

    public class GYViewSet extends GYViewSetSet<GYImageView, GYViewSet> {
        public Paint mPaint;
        public Bitmap mSelectedIcon;
        public Bitmap mNormalIcon;
        public Rect mSelectedRect;
        public Rect mNormalRect;
        public int mSelectedAlpha = 0;

        public GYViewSet(View view) {
            super(view);
            mPaint = new Paint();
        }

        public GYViewSet setMaxWidthPx(int maxWidthPx) {
            GYImageView.super.setMaxWidth(maxWidthPx);
            return this;
        }

        public GYViewSet setMaxWidthDp(int maxWidthDp) {
            return setMaxWidthPx(GYDisplayUtils.dp2px(getContext(), maxWidthDp));
        }

        public GYViewSet setMaxHeightPx(int maxHeightPx) {
            GYImageView.super.setMaxHeight(maxHeightPx);
            return this;
        }

        public GYViewSet setMaxHeightDp(int maxHeightDp) {
            return setMaxHeightPx(GYDisplayUtils.dp2px(getContext(), maxHeightDp));
        }

        public GYViewSet setMinimumWidthPx(int minWidthPx) {
            GYImageView.super.setMinimumWidth(minWidthPx);
            return this;
        }

        public GYViewSet setMinimumWidthDp(int minWidthDp) {
            return setMinimumWidthPx(GYDisplayUtils.dp2px(getContext(), minWidthDp));
        }

        public GYViewSet setMinimumHeightPx(int minHeightPx) {
            GYImageView.super.setMinimumHeight(minHeightPx);
            return this;
        }

        public GYViewSet setMinimumHeightDp(int minHeightDp) {
            return setMinimumHeightPx(GYDisplayUtils.dp2px(getContext(), minHeightDp));
        }

        public GYViewSet setScaleY(float scaleY) {
            GYImageView.super.setScaleY(scaleY);
            return this;
        }

        public GYViewSet setScaleX(float scaleX) {
            GYImageView.super.setScaleX(scaleX);
            return this;
        }

        public GYViewSet setScaleType(ScaleType scaleType) {
            GYImageView.super.setScaleType(scaleType);
            return this;
        }

        public GYViewSet setTransformImages(Drawable normalDrawable, Drawable selectDrawable, int width, int height) {
            mNormalIcon = GYDisplayUtils.toBitmap(normalDrawable);                            // 拿到原图
            mSelectedIcon = GYDisplayUtils.toBitmap(selectDrawable);
            mNormalRect = new Rect(0, 0, width, height);                   //拿到画板的大小
            mSelectedRect = new Rect(0, 0, width, height);
            return this;
        }

        /**
         * 当滑动的时候来调用此方法
         *
         * @param offset
         */
        public GYViewSet setTransform(float offset) {
            mSelectedAlpha = (int) (255 * (offset));
            invalidate();  // 此方法调用就会从新走 onDraw方法
            return this;
        }
    }

    public GYImageBuilder getImageBuilder() {
        if (imageBuilder == null) {
            imageBuilder = new GYImageBuilder();
        }
        return imageBuilder;
    }

    private GYImageBuilder imageBuilder;

    public class GYImageBuilder {
        public Drawable imageSrc;
        public int imageRadiusLeftTopPx = 0;
        public int imageRadiusRightTopPx = 0;
        public int imageRadiusRightBottomPx = 0;
        public int imageRadiusLeftBottomPx = 0;
        public int imageDisplayStyle = GYDisplayInfo.DisplayStyle.NONE;
        public int imageEdgeWidthPx = 0;//圆形边框宽度
        public int imageEdgeColor = Color.TRANSPARENT;//圆形边框颜色
        public boolean imageEdgeOver = false;//圆形边框是否覆盖在图片上

        public GYImageBuilder setImageDisplayStyle(@GYDisplayInfo.DisplayStyle int imageDisplayStyle) {
            this.imageDisplayStyle = imageDisplayStyle;
            return this;
        }

        public GYImageBuilder setImageRadiusPx(int imageRadiusPx) {
            setImageRadiusLeftTopPx(imageRadiusPx);
            setImageRadiusRightTopPx(imageRadiusPx);
            setImageRadiusRightBottomPx(imageRadiusPx);
            setImageRadiusLeftBottomPx(imageRadiusPx);
            return this;
        }

        public GYImageBuilder setImageRadiusDp(int imageRadiusDp) {
            return setImageRadiusPx(GYDisplayUtils.dp2px(getContext(), imageRadiusDp));
        }

        public GYImageBuilder setImageRadiusLeftTopPx(int imageRadiusLeftTopPx) {
            this.imageRadiusLeftTopPx = imageRadiusLeftTopPx;
            return this;
        }

        public GYImageBuilder setImageRadiusLeftTopDp(int imageRadiusLeftTopDp) {
            return setImageRadiusLeftTopPx(GYDisplayUtils.dp2px(getContext(), imageRadiusLeftTopDp));
        }

        public int getImageRadiusLeftTopPx() {
            return imageRadiusLeftTopPx;
        }

        public GYImageBuilder setImageRadiusRightTopPx(int imageRadiusRightTopPx) {
            this.imageRadiusRightTopPx = imageRadiusRightTopPx;
            return this;
        }

        public GYImageBuilder setImageRadiusRightTopDp(int imageRadiusRightTopDp) {
            return setImageRadiusRightTopPx(GYDisplayUtils.dp2px(getContext(), imageRadiusRightTopDp));
        }

        public int getImageRadiusRightTopPx() {
            return imageRadiusRightTopPx;
        }

        public GYImageBuilder setImageRadiusRightBottomPx(int imageRadiusRightBottomPx) {
            this.imageRadiusRightBottomPx = imageRadiusRightBottomPx;
            return this;
        }

        public GYImageBuilder setImageRadiusRightBottomDp(int imageRadiusRightBottomDp) {
            return setImageRadiusRightBottomPx(GYDisplayUtils.dp2px(getContext(), imageRadiusRightBottomDp));
        }

        public int getImageRadiusRightBottomPx() {
            return imageRadiusRightBottomPx;
        }

        public GYImageBuilder setImageRadiusLeftBottomPx(int imageRadiusLeftBottomPx) {
            this.imageRadiusLeftBottomPx = imageRadiusLeftBottomPx;
            return this;
        }

        public GYImageBuilder setImageRadiusLeftBottomDp(int imageRadiusLeftBottomDp) {
            return setImageRadiusLeftBottomPx(GYDisplayUtils.dp2px(getContext(), imageRadiusLeftBottomDp));
        }

        public int getImageRadiusLeftBottomPx() {
            return imageRadiusLeftBottomPx;
        }

        public GYImageBuilder setImageEdgeWidthPx(int imageEdgeWidthPx) {
            this.imageEdgeWidthPx = imageEdgeWidthPx;
            return this;
        }

        public GYImageBuilder setImageEdgeWidthDp(int imageEdgeWidthDp) {
            return setImageEdgeWidthPx(GYDisplayUtils.dp2px(getContext(), imageEdgeWidthPx));
        }

        public GYImageBuilder setImageEdgeColor(int imageEdgeColor) {
            this.imageEdgeColor = imageEdgeColor;
            return this;
        }

        public GYImageBuilder setImageEdgeOver(boolean imageEdgeOver) {
            this.imageEdgeOver = imageEdgeOver;
            return this;
        }

        public boolean isImageEdgeOver() {
            return imageEdgeOver;
        }

        private int getPx(int px) {
            return px < 0 ? WRAP_CONTENT : px;
        }

        public GYImageBuilder setImageResource(int resId) {
            return setImageDrawable(getResources().getDrawable(resId));
        }

        public GYImageBuilder setImageDrawable(@Nullable Drawable drawable) {
            imageSrc = drawable;
            return this;
        }

        @GYDisplayInfo.DisplayStyle
        public int getImageType() {
            return imageDisplayStyle;
        }

        public GYImageView build() {
            GYImageView.this.setImageDrawable(GYDisplayUtils.newBuilder()
                    .setNormal(imageSrc)
                    .setRadiusLeftTopPx(imageRadiusLeftTopPx)
                    .setRadiusRightTopPx(imageRadiusRightTopPx)
                    .setRadiusRightBottomPx(imageRadiusRightBottomPx)
                    .setRadiusLeftBottomPx(imageRadiusLeftBottomPx)
                    .setEdgeWidthPx(imageEdgeWidthPx)
                    .setEdgeColor(imageEdgeColor)
                    .setEdgeOver(imageEdgeOver)
//                    .setChangeEffect(changeEffect)
                    .setDisplayStyle(imageDisplayStyle)
                    .buildDrawable());
            return GYImageView.this;
        }
    }

    /**
     * ================================================================================================
     */
    @Override
    protected void onDraw(Canvas canvas) {
        if (getViewSet().mNormalIcon != null && getViewSet().mNormalRect != null && getViewSet().mSelectedIcon != null && getViewSet().mSelectedRect != null) {
            getViewSet().mPaint.setAlpha(255 - getViewSet().mSelectedAlpha);
            canvas.drawBitmap(getViewSet().mNormalIcon, null, getViewSet().mNormalRect, getViewSet().mPaint); //开始在画板上画原图
            // 也就是在这个控件上画bitmap
            getViewSet().mPaint.setAlpha(getViewSet().mSelectedAlpha);
            canvas.drawBitmap(getViewSet().mSelectedIcon, null, getViewSet().mSelectedRect, getViewSet().mPaint);
        }
        super.onDraw(canvas);
    }
}
